// index.js
// 测试图片
const url = 'http://cloudcdn.tch1217.com/Fm7_gXyenIkDuJAyenmc8wju8WaQ?imageView2/1/w/50/h/50/q/100'
// 获取应用实例
const app = getApp()
//引入接口
import {
	homelist,
	bindName,
	getSlide,
	shareSave
} from '../../api/cloud.js';
import {randomNumber} from '../../utils/util.js';
// 节流
let t = null
Page({
	data: {
		slideImg: null,
		indicatorDots: false,
		vertical: false,
		autoplay: true,
		interval: 2000,
		duration: 500,
		//是否显示个人图标
		is_info:true,
		//确认头像授权弹窗
		iosDialog1: false,
		// 图像集合
		imageList: [],
		//是否有图像
		condition: false,
		//是否可以下拉
		down: true,
		//标题加载状态
		nbLoading: false,
		hideLoading: false,
		loading: false,
		limit: 50,
		page: 1

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getLunbo();
		if(options.code){
			this.share(3,options.code)
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
    // console.log('onReady')
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		// console.log('onShow')
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {
		this.setData({
			imageList: [],
			condition: false,
			down: true,
			nbLoading: false,
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {
		// console.log('onHide')
	},
	
	/**
	 * 判断单点事件和长按
	 */
	multipleTap(e) {
		this.bigimg(e);
	},
	/**
	 * 获取轮播
	 */
	getLunbo(){
		getSlide().then(res=>{
			this.setData({
				slideImg: res.data
			})
			this.getlist();
		}).catch(err=>{})
	},
	/**
	 * 获取相册列表
	 */
	getlist() {
		let nickName =wx.getStorageSync('nickName')
		let page = this.data.page
		if(page == 3)
		{
			if(!nickName){
				this.setData({
					iosDialog1: true,
				})
				return
			}
		}
		
		this.setData({
			nbLoading: true
		})
		homelist({
			limit: this.data.limit,
			page: this.data.page,
		}).then(rs => {
			let length = rs.data.length
			if (length > 0) {
				let page = this.data.page+1
				this.setData({
					imageList: [...this.data.imageList, ...rs.data],
					condition: true,
					nbLoading: false,
					page:page
				})
				// this.getYTdtime()
			}
			if (length < 50) {
				this.setData({
					down: false,
					nbLoading: false
				})
			}
		})
	},
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		if (this.data.down) {
			this.getlist()
		} else {
			wx.showToast({
				title: '没有数据了',
				icon: 'error'
			})
		}
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		wx.stopPullDownRefresh()
		this.setData({
			imageList: [],
			condition: false,
			down: true,
			nbLoading: false,
			page:1
		});
		this.getlist()
	},
	/**
	 * 放大图片查看
	 */
	bigimg(e) {
		let data = this.data.imageList
		let item = e.target.dataset.item
		let imgurl = data[item]['urlbig']
		let arr = [imgurl]
		// let listurl = data.filter(index => {
		// 	arr.push(index.urlbig)
		// 	return index.url
		// })
		wx.previewImage({
			current: imgurl, // 当前显示图片的 http 链接
			urls: arr // 需要预览的图片 http 链接列表/
		})
	},
	/**
	 * 关闭弹窗
	 */
	 close() {
		this.setData({
			iosDialog1: false,
		});
	},
	/**
	 * 删除弹窗
	 */
	openIOS1() {
		this.setData({
			iosDialog1: true,
		});
	},
	/**
   * 获取授权头像
   */
	 getHeadimg(){
		this.setData({
			iosDialog1: false,
		});
		wx.getUserProfile({
			  desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			  success: (res) => {
				wx.setStorageSync('nickName',res.userInfo.nickName);
				wx.setStorageSync('avatarUrl',res.userInfo.avatarUrl);
				bindName({
				   nickname:res.userInfo.nickName,
				   headimgurl:res.userInfo.avatarUrl
				}).then(rs=>{
				   wx.showToast({
					 title: rs.msg,
					 icon: 'success'
				   }) 
				})
			  }
			}) 
	 },
	 /**
	  * 跳转到个人中心
	  */
	 pageme(){
		wx.navigateTo({
			url: '/pages/me/me'
		  })
	 },
	 onPageScroll(e) {
		
        this.showInfo()
    },
	/**
	 * 滑动隐藏图标
	 */
	showInfo(){
		
		if(this.t !==null)
		{
			clearTimeout(this.t)
		}
		if(this.data.is_info == true)
		{
			console.log(this.t)
			this.setData({
				is_info:false
			})
		}
		this.t = setTimeout(()=>{
			this.setData({
				is_info:true
			})
		},3000)
	},
	/**
	 * 分享记录
	 */
	share(type ,code){
		let pages = getCurrentPages()
		let currentPage = pages[pages.length-1]
		let path = currentPage.route
		// let code =randomNumber()
		// console.log(code)
		shareSave({
			path:path,
			type:type,
			code:code
		}).then(res=>{
			console.log(res)
		}).catch(err=>{
			console.log(err)
		})
	},
	onShareAppMessage(){
		let code =randomNumber()
		this.share(1,code)
		let path = 'pages/index/index?code='+code
		return{
			path:path,
		}
	},
  	onShareTimeline(){
		let code =randomNumber()
		this.share(2,code)
		let path = 'pages/index/index?code='+code
		return{
			path:path,
		}
	}
})
